package com.jkgh.tnh.rs {

	import com.jkgh.remedium.*;
	import com.jkgh.remedium.objects.*;
	import com.jkgh.remedium.objects.messages.*;
	import com.jkgh.remedium.objects.messages.ins.*;
	import com.jkgh.remedium.objects.messages.out.*;
	import com.jkgh.remedium.states.*;
	import com.jkgh.remedium.tools.*;

	/**
	 * The access class of this system.
	 * 
	 * @author RS2FGenerator
	 *
	 */
	public class TNHAccess extends RemediumDataObject {

		/**
		 * Version of this TNH Server.
		 */
		public var version:int;

		/**
		 * Alternative access object.
		 */
		public var alternative:TNHAccess;

		/**
		 * Last few messages of the day.
		 */
		public var motds:String[];

		/**
		 * General purpose constructor.
		 * 
		 * @param version Version of this TNH Server.
		 * @param alternative Alternative access object.
		 * @param motds Last few messages of the day.
		 */
		public function TNHAccess(version:int = 0, alternative:TNHAccess = null, motds:String[] = null) {
			this.version = version;
			this.alternative = alternative;
			this.motds = motds;
		}

		// --------------------------------------------------------------------------------

		override public function produceContent(remedium:RemediumSystem, message:RemediumOutMessage):void {
			remedium.consumeInt(version);
			message.produceObject(alternative, remedium);
			RemediumTools.produceStringArray(remedium, motds);
		}

		public static function createConsumingState(message:RemediumInMessage, future:Function):RemediumState {
			return new GatheringState(1, function(remedium:RemediumSystem):void {

				switch (remedium.produceByte()) {

					case RemediumConstants.NULL:
						future(null);
						break;

					case RemediumConstants.HISTORY:
						future(TNHAccess(message.fromHistory(remedium.produceInt())));
						break;

					case RemediumConstants.VALUE:
						consumeContent(remedium, message, future);
						break;
				}
			});
		}

		public static function createVectorConsumingState(message:RemediumInMessage, future:Function):RemediumState {
			return new GatheringState(4, function(remedium:RemediumSystem):void {

				var length:int = remedium.produceInt();
				if (length == -1) {
					future(null);
				} else if (length == 0) {
					future(new Vector.<TNHAccess>());
				} else {
					var i:int = 0;
					var result:Vector.<TNHAccess> = new Vector.<TNHAccess>(length, true);
					var onO:Function = function(o:TNHAccess):void {
						result[i++] = o;
						if (i == length) {
							future(result);
						} else {
							remedium.setState(createConsumingState(message, onO));
						}
					};
					remedium.setState(createConsumingState(message, onO));
				}
			});
		}

		private static function consumeContent(remedium:RemediumSystem, message:RemediumInMessage, future:Function):void {
			var ret:TNHAccess = new TNHAccess();
			message.toHistory(ret);
			RemediumTools.consumeInt(remedium, function(rversion:int):void {

				ret.version = rversion;
				remedium.setState(TNHAccess.createConsumingState(message, function(ralternative:TNHAccess):void {

					ret.alternative = ralternative;
					RemediumTools.consumeStringArray(remedium, function(rmotds:Vector.<String>):void {

						ret.motds = rmotds;
						future(ret);
					});
				}));
			});
		}
	}
}
